package com.wxl.micro.service.order.controller;

import com.alibaba.fastjson2.JSON;
import com.wxl.micro.service.order.feign.StockFeignClient;
import com.wxl.micro.service.order.feign.WmsFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
@RefreshScope // 监听nacos配置中心的变化  和@Value配合使用   需要动态刷新的类都要加  但是不能加载定时任务的相关注解类上 如@Scheduled
public class OrderController {

    //从nacos配置中心获得配置   只有@Value，启动时获取值，但不能动态  需要使用 @RefreshScope
    @Value("${redis.password}")
    private String password;
    @Value("#{'${redis.ips}'.split(',')}")
    private List<String> ips;
    @Value("${order.biz}")
    private String biz;
    @Value("${extension.group:dgroup}")
    private String group;

    @Autowired
    private StockFeignClient stockFeignClient;

    @Autowired
    private WmsFeignClient wmsFeignClient;

    @GetMapping("/config")
    public String getConfig(){
        System.out.println("接口获取配置中心数据password:"+password + " ips：" + JSON.toJSONString(ips)+" biz:"+biz+" group:"+group);
        return "接口获取配置中心数据password:"+password + " ips：" + JSON.toJSONString(ips)+" biz:"+biz+" group:"+group;
    }

    @GetMapping("/save")
    public String save(@RequestParam String username, @RequestParam String sku){
        String orderId = System.currentTimeMillis()+"";

        String query = stockFeignClient.query(sku);

        String delivery = wmsFeignClient.delivery(orderId);

        System.out.println("user:"+username+"下单成功。sku:"+sku);

        return "user:"+username+"下单成功。sku:"+sku + "  stock:" + query + "  wms:" + delivery;
    }

    @GetMapping("/query")
    public String save(@RequestParam String id){


        String query = stockFeignClient.query(id);

        return "order-query-id:"+id;
    }


    @GetMapping("/test")
    public String test(@RequestParam String type){

        if("timeout".equals(type)){
            return wmsFeignClient.timeout();
        }else if("exception".equals(type)){
            return wmsFeignClient.exception();
        }else if("post".equals(type)){
            return wmsFeignClient.post("post");
        }

        return wmsFeignClient.delivery("delivery");
    }

}
